﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfServiceDuplex
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class CalculatorService : ICalculatorDuplex
    {
        double result;
        string equation;

        ICalculatorDuplexCallback callback = null;

        public CalculatorService()
        {
            result = 0.0;
            equation = result.ToString();
            callback = OperationContext.Current.GetCallbackChannel<ICalculatorDuplexCallback>();
        }

        public void Clear()
        {
            equation +="=" + result.ToString();
            callback.Equation(equation);
            result = 0.0;
            equation = result.ToString();
        }

        public void AddTo(double n)
        {
            result += n;
            equation += "+" + n.ToString() + " then ";
            callback.Equals(result);
        }

        public void SubtractFrom(double n)
        {
            result -= n;
            equation += "-" + n.ToString() + " then ";
            callback.Equals(result);
        }

        public void MultiplyBy(double n)
        {
            result *= n;
            equation += "*" + n.ToString() + " then ";
            callback.Equals(result);
        }

        public void DivideBy(double n)
        {
            result /= n;
            equation += "/" + n.ToString() + " then ";
            callback.Equals(result);
        }
    }


}
